package main.mainframe;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;

import main.staff.CreateTypesSql;
import main.util.PYUtil;
import main.util.ReplaceSQLString;
import main.util.SqlConstants;

public class ReplaceString {
	
	public static ReplaceSQLString rss=new ReplaceSQLString();
	public static BigInteger sequence;
	
	static{
		String date=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		sequence=new BigInteger(date+"0000000000000001");
	}
	
	//替换sql里面的字符串
	public static void main(String[] args){
		File origin=new File("D:/t_ucp_menu_(生产URL).sql");
		File out=new File("D:/t_ucp_mainframeconfigitem.sql");
		createMainframe(origin,out);
	}

	public static void createMainframe(File origin,File out){
		
		
		try {
			FileWriter fw = new FileWriter(out);
			FileInputStream fis = new FileInputStream(origin);
			BufferedReader bh = new BufferedReader(new InputStreamReader(fis));
			String sql = bh.readLine();
			SqlConstants.topWriteHead(fw);
			int i=1;
			while (sql!= null) {
				String newSql=getManiframeSql2(sql);
				fw.write(newSql);
				sql = bh.readLine();
				System.out.println(i+"。。。。。。"+newSql);
				i++;
			}
			fw.close();
			bh.close();
		} catch (Exception e) {
		}
	}
	
	public static String getManiframeSql(String sql){
		String moduleId=rss.getColNameValue(sql,"MODULEID");
		if(moduleId.equals("")){
			return "";
		}
		if(!moduleId.equals("210")){
			return "";
		}
		String menuId=rss.getColNameValue(sql, "MENUID");
		if(!menuId.matches("[0-9]{11,}+")){
			return "";
		}
		
		String newSql= "INSERT INTO `t_ucp_mainframeconfigitem` (CONFIGID,MENUID,PARENTID,toolbarType) "
					+ "values ('"+sequence+"','"+menuId+"','"+menuId.substring(0, menuId.length()-3)+"','01');\n";
		
		sequence=sequence.add(new BigInteger("1"));
		//System.out.println(sequence+"、成功"+menuId);
		return newSql;
	}
	
	public static String getManiframeSql2(String sql){
		String PARENTID=rss.getColNameValue(sql,"PARENTID");
		if(PARENTID.equals("")){
			return "";
		}
		if(!PARENTID.matches("[0|2|8]{8}[2|3]{1}")){
			return "";
		}
		String menuId=rss.getColNameValue(sql, "MENUID");
		if(!menuId.matches("[0-9]{11,}+")){
			return "";
		}
		
		String newSql= "INSERT INTO `t_ucp_mainframeconfigitem` (CONFIGID,MENUID,PARENTID,toolbarType) "
					+ "values ('"+sequence+"','"+menuId+"','"+menuId.substring(0, menuId.length()-3)+"','00');\n";
		
		sequence=sequence.add(new BigInteger("1"));
		//System.out.println(sequence+"、成功"+menuId);
		return newSql;
	}
}
